package com.example.demo.controller;

import com.example.demo.entity.Goods;
import com.example.demo.entity.Orders;
import com.example.demo.entity.User;
import com.example.demo.service.OrderService;
import com.example.demo.vo.Result;
import com.example.demo.vo.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import lombok.NonNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author DLX
 * @data 2024/5/31 23:16
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;


    @Operation(summary = "生成订单")
    @PostMapping("/add")
    public Result add(@Parameter(name = "gid", description = "商品id", required = true) @NonNull Long gid, @Parameter(name = "uid", description = "用户id", required = true) @NonNull Long uid, @Parameter(name = "addressId", description = "地址id", required = true) @NonNull Long addressId, @Parameter(name = "num", description = "商品数量", required = true) @NonNull Long num, @Parameter(name = "address", description = "发货地址", required = true) String address) {

        boolean res = orderService.add(gid, uid, addressId, num, address);

        if (res) {
            return Result.ok(ResultCode.SUCCESS);
        } else {
            return Result.error(ResultCode.CLIENT_A9999);
        }
    }


    @Operation(summary = "更改订单状态")
    @PutMapping("/changeStatus")
    public Result update(@Parameter(name = "status", description = "订单状态", required = true) @NonNull String status, @Parameter(name = "oid", description = "订单id", required = true) @NonNull Long oid) {
        orderService.update(status, oid);
        return Result.ok();
    }

    @Operation(summary = "根据用户id拿到对应用户的订单信息")
    @GetMapping("/getOrderById")
    public Result<List<Orders>> getOrderById(@Parameter(name = "uid", description = "用户id") @NonNull Long uid) {
        // 调用service
        List<Orders> ordersList = orderService.getOrderById(uid);
        // 成功返回
        return Result.ok(ordersList);
    }


    @Operation(summary = "根据商家id拿到对应用户的订单信息")
    @GetMapping("/getOrderBySid")
    public Result<List<Orders>> getOrderBySid(@Parameter(name = "sid", description = "商家id") @NonNull Long sid) {
        // 调用service
        List<Orders> ordersList = orderService.getOrderBySid(sid);
        // 成功返回
        return Result.ok(ordersList);
    }


    @Operation(summary = "根据订单id查询订单")
    @GetMapping("/getOrderByOid")
    public Result<Orders> getOrderByOid(@Parameter(name = "oid", description = "订单id") @NonNull Long oid) {
        Orders orders = orderService.getOrderByOid(oid);
        return Result.ok(orders);
    }

}


